package com.wondertek.mist;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.baidu.android.pushservice.PushConstants;
import com.wondertek.mist.utils.StringUtils;

public class MessageReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.i("MessageReceiver",intent.getAction());
		if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) {
			//获取消息内容
			String message = intent.getExtras().getString(PushConstants.EXTRA_PUSH_MESSAGE_STRING);
			/*
			Intent responseIntent =  new Intent(Utils.ACTION_MESSAGE);
			responseIntent.putExtra(Utils.EXTRA_MESSAGE, message);
			responseIntent.setClass(context, Mist.class);
			responseIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(responseIntent);
			*/

		} else if (intent.getAction().equals(PushConstants.ACTION_RECEIVE)) {
			//处理绑定等方法的返回数据
			//PushManager.startWork()的返回值通过PushConstants.METHOD_BIND得到
	//		StartActivity.isLogin = true;
			//获取方法
			final String method = intent.getStringExtra(PushConstants.EXTRA_METHOD);
			//方法返回错误码。若绑定返回错误（非0），则应用将不能正常接收消息。
			//绑定失败的原因有多种，如网络原因，或access token过期。
			//请不要在出错时进行简单的startWork调用，这有可能导致死循环。
			//可以通过限制重试次数，或者在其他时机重新调用来解决。
			final int errorCode = intent.getIntExtra(PushConstants.EXTRA_ERROR_CODE,PushConstants.ERROR_SUCCESS);
			//返回内容
			final String content = new String(intent.getByteArrayExtra(PushConstants.EXTRA_CONTENT));
			Log.i("MessageReceiver","content="+content);
			if(errorCode == 0){				
				try {
					JSONObject jsonObj = new JSONObject(content);
					JSONObject responseParams = jsonObj.getJSONObject("response_params");
					Log.i("MessageReceiver","responseParams="+responseParams.toString());
					if (responseParams.has("user_id")) {
						Utils.saveString(context, "push.user_id", responseParams.getString("user_id"));
						Utils.saveString(context, "push.channel_id", responseParams.getString("channel_id"));
						
					} 
				} catch (JSONException e) {					
					e.printStackTrace();
				}
			}		
		//通知用户点击事件处理
		} else if (intent.getAction().equals(PushConstants.ACTION_RECEIVER_NOTIFICATION_CLICK)) {
		
			Intent aIntent = new Intent(Utils.ACTION_NOTI);
			aIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			aIntent.setClass(context, MistActivity.class);
			String title = intent.getStringExtra(PushConstants.EXTRA_NOTIFICATION_TITLE);
			aIntent.putExtra(PushConstants.EXTRA_NOTIFICATION_TITLE, title);
			String content = intent.getStringExtra(PushConstants.EXTRA_NOTIFICATION_CONTENT);
			aIntent.putExtra(PushConstants.EXTRA_NOTIFICATION_CONTENT, content);
			String extra = intent.getStringExtra(PushConstants.EXTRA_EXTRA);
			aIntent.putExtra(PushConstants.EXTRA_EXTRA, extra);
			context.startActivity(aIntent);
			
			
		}
		
		String killMsg=Utils.getMetaValue(context, "mam.killMsg");
		if(!StringUtils.isBlank(killMsg) && killMsg.equalsIgnoreCase(intent.getAction())){
			Log.i("MessageReceiver", "exit");
			Utils.reset(context);
			//System.exit(0);
			
			int pid=android.os.Process.myPid();
			android.os.Process.killProcess(pid);
		}
	}

}
